package com.pi2e.view.widget;

import com.pi2e.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

/**
 * @ClassName: SwitchView
 * @Description: TODO(自定义网络开关)
 * @author luotao
 * @date 2016年9月29日 上午11:00:52
 *
 */
public class SwitchView extends View implements OnTouchListener {
	// 开关开启时背景，开关关闭时背景，开关按钮
	private Bitmap mswitch_on_bg, mswitch_off_bg, mswitch_btn_on,
			mswitch_btn_off;
	// 手指按下时的水平坐标X，当前的水平坐标X
	private float previousX, currentX;
	/* 是否在滑动 */
	private boolean iSlibing;
	/** 开关状态 */
	private boolean isSwitchOn;

	private Bitmap tempBtn;
	private Bitmap tempBg;
	private SwitchListener mSwitchListener;
//默认打开
	private boolean isOpen = true ;
	//
	public SwitchView(Context context) {
		super(context);
	}

	public void initSwitch(boolean isOpen) {
		if (isOpen) {
			tempBtn = mswitch_btn_on;
			tempBg = mswitch_on_bg;
		} else {
			tempBtn = mswitch_btn_off;
			tempBg = mswitch_off_bg;
		}
		isSwitchOn = isOpen;
	}

	public SwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		// TODO Auto-generated constructor stub
	}

	public SwitchView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public void initSwitchState(Canvas canvas,boolean switchState){
		Matrix matrix = new Matrix();
		Paint paint = new Paint();
		if (switchState) {
			canvas.drawBitmap(mswitch_on_bg, matrix, paint); // 绘制打开背景
			canvas.drawBitmap(tempBtn, tempBg.getWidth() / 2, 0, paint);
		}else{
			canvas.drawBitmap(mswitch_off_bg, matrix, paint); // 绘制关闭背景
			canvas.drawBitmap(tempBtn, 0, 0, paint);
		}
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		Matrix matrix = new Matrix();
		Paint paint = new Paint();
		// 在左边
		if (currentX < (tempBg.getWidth() / 2)) {
			canvas.drawBitmap(mswitch_off_bg, matrix, paint); // 绘制关闭背景
			
		} else {
			canvas.drawBitmap(mswitch_on_bg, matrix, paint); // 绘制打开背景
			
		}

		Bitmap tempSwitch = null;
		// 滑动按钮的左边坐标
		float left_SlipBtn = 0;
		// 判断当前是否正在滑动
		if (iSlibing) {
			if (currentX > tempBg.getWidth()) {
				left_SlipBtn = tempBg.getWidth() - tempBtn.getWidth();
			} else {
				left_SlipBtn = currentX - tempBtn.getWidth() / 2;
			}
		} else {
			// 根据当前的开关状态设置滑动按钮的位置

		}

		// 对滑动按钮的位置进行异常判断
		if (left_SlipBtn < 0) {
			left_SlipBtn = 0;
		} else if (left_SlipBtn > mswitch_off_bg.getWidth()
				- mswitch_btn_off.getWidth()) {
			left_SlipBtn = mswitch_off_bg.getWidth()
					- mswitch_btn_off.getWidth();
		}

		canvas.drawBitmap(tempBtn, left_SlipBtn, 0, paint);

	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouch(View v, MotionEvent event) {

		int eventActiont = event.getAction();
		switch (eventActiont) {
		case MotionEvent.ACTION_MOVE:
			currentX = event.getX();

			break;
		case MotionEvent.ACTION_DOWN:
			if (event.getX() > tempBtn.getWidth()
					|| event.getY() > mswitch_on_bg.getHeight()) {
				return false;
			}
			previousX = event.getX();
			currentX = previousX;
			iSlibing = true;
			break;
		case MotionEvent.ACTION_UP:
			iSlibing = false;
			// 松开前开关的状态
			boolean previousSwitchState = isSwitchOn;
			Bitmap previoutsBtn = tempBtn;
			Bitmap previoutsBg = tempBg;
			if (currentX > tempBtn.getWidth() / 2) {
				isSwitchOn = true; // 打开
				tempBtn = mswitch_btn_on;
				tempBg = mswitch_on_bg;

			} else {
				isSwitchOn = false;// 关闭
				tempBtn = mswitch_btn_off;
				tempBg = mswitch_on_bg;

			}
			if (mSwitchListener != null && previousSwitchState != isSwitchOn) {
				if (isSwitchOn) {
					mSwitchListener.switchOnListener();
				} else {
					mSwitchListener.switchOffListener();
				}

			}
			break;

		}
		invalidate();
		return true;
	}

	/**
	 * @Title: setImageResource
	 * @Description: TODO(设置开关相关图片)
	 * @param @param switch_btn_on 开关打开时按钮图片
	 * @param @param switch_btn_off 开关关闭时按钮图片
	 * @param @param switch_bg_on 开关打开背景图片
	 * @param @param switch_bg_off 开关关闭背景图片
	 * @return void 返回类型
	 * @throws
	 */
	public void setImageResource(int switch_btn_on, int switch_btn_off,
			int switch_bg_on, int switch_bg_off) {
		mswitch_btn_on = BitmapFactory.decodeResource(getResources(),
				switch_btn_on);
		mswitch_btn_off = BitmapFactory.decodeResource(getResources(),
				switch_btn_off);
		mswitch_off_bg = BitmapFactory.decodeResource(getResources(),
				switch_bg_off);
		mswitch_on_bg = BitmapFactory.decodeResource(getResources(),
				switch_bg_on);
	}

	public void init() {

	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	public interface SwitchListener {
		// 开关打开监听
		void switchOnListener();

		// 开关关闭监听
		void switchOffListener();
	}

	public SwitchListener getmSwitchListener() {
		return mSwitchListener;
	}

	public void setmSwitchListener(SwitchListener mSwitchListener) {
		this.mSwitchListener = mSwitchListener;
	}

}
